﻿using System;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace DotNetImage.Imaging.TransformFilters
{
    public class ResizeFilter<TPixel, TColorChannel> : FilterBase
    {
        public ResizeFilter() { HasMultiThreadingSupport = true; RequiresInternalRefresh = true; }

        [ParameterAttribute(Name = "ScaleX", DefaultValue = 1, MinValue = 0, MaxValue=Int32.MaxValue)]
        public double ScaleX { get; set; }

        [ParameterAttribute(Name = "ScaleY", DefaultValue = 1, MinValue = 0, MaxValue = Int32.MaxValue)]
        public double ScaleY { get; set; }

        [ParameterAttribute(Name = "Quality", DefaultValue = (double)BitmapScalingMode.HighQuality)]
        public BitmapScalingMode Quality { get; set; }


        protected override void ValidateArguments()
        {
            Validator.ValidateInclusiveRange("ScaleX", ScaleX, 0, Int32.MaxValue);
            Validator.ValidateInclusiveRange("ScaleY", ScaleY, 1, Int32.MaxValue);
        }

        protected override void ProcessPerPixel(DotNetImage bmp)
        {
            TransformedBitmap tb = new TransformedBitmap();
            tb.BeginInit();
            tb.Source = bmp.BitmapSource;
            var transform = new System.Windows.Media.ScaleTransform(ScaleX, ScaleY);
            tb.Transform = transform;
            tb.EndInit();  // "Transform must be a combination of scales, flips, and 90 degree rotations"
        }
    }
}
